package com.platform.enums;

import com.platform.comm.constants.EnumOption;
import com.platform.comm.enums.IEnumOption;
import lombok.Getter;

import java.util.Arrays;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * Description: 菜单启用，禁用，删除枚举
 *
 * @author libin Created on 2019/7/16
 **/
@EnumOption
public enum FunctionStatus implements IEnumOption<String> {
    /**
     * 禁用
     */
    N(1, "禁用"),

    /**
     * 启用
     */
    Y(0, "启用"),

    /**
     * 删除
     */
    D(2, "删除");

    @Getter
    private final int code;
    @Getter
    private final String text;

    FunctionStatus(int code, String text) {
        this.code = code;
        this.text = text;
    }

    /**
     * 枚举项 “键”
     *
     * @return key
     */
    @Override
    public String key() {
        return Objects.toString(this.code);
    }

    /**
     * 枚举项 “值”
     *
     * @return value
     */
    @Override
    public String value() {
        return this.text;
    }

    /**
     * 枚举options
     */
    @Override
    public Map<String, String> enums() {
        return Arrays.stream(values()).collect(Collectors.toMap(IEnumOption::key, IEnumOption::value));
    }

    public static FunctionStatus getByValue(Integer value) {
        if (value.equals(N.code)) {
            return N;
        } else if (value.equals(Y.code)) {
            return Y;
        } else if (value.equals(D.code)) {
            return D;
        } else {
            return null;
        }
    }

    public static FunctionStatus parse(String name) {
        if (FunctionStatus.N.name().equals(name)) {
            return FunctionStatus.N;
        } else if (FunctionStatus.Y.name().equals(name)) {
            return FunctionStatus.Y;
        }
        return FunctionStatus.D;
    }

}
